package net.aaa.hotel.controller;

import com.alibaba.fastjson.JSON;
import net.aaa.hotel.dao.ChukuDao;
import net.aaa.hotel.dao.impl.ChukuDaoImpl;
import net.aaa.hotel.entity.PageInfo;
import net.aaa.hotel.util.LayDate;
import net.aaa.hotel.util.LayuiTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chuku")
public class ChukuSpringController {
    @Autowired
    private ChukuDao dao = new ChukuDaoImpl();
    @RequestMapping("/query")
    public LayuiTable query(PageInfo pagl){
        List<Map> maps = dao.query(pagl);
        int count = dao.count(pagl);
        return new LayuiTable(count,maps);
    }
    @RequestMapping("/queryId")
    public LayuiTable queryId(Integer id){
        List<Map> map = dao.queryCuku(id);
        return new LayuiTable(map.size(),map);
    }
    @RequestMapping("/queryB")
    public List<Map> queryB(){
        List<Map> maps = dao.queryB();
        return maps;
    }
    @RequestMapping("/add")
    public int add(HttpServletRequest request){
        // 将接收到的表单数据转化成 map 用于添加入库单
        Map map = (Map) JSON.parse(request.getParameter("data"));
        // 将接收的入库表格数据 转化成 List<Map> 用于添加入库详情单 和 让图书数量增加
        List<Map> ta = (List<Map>) JSON.parse(request.getParameter("tableData"));

        int a = dao.addCu(map);

        int b=0;

        for (Map map1 : ta) {
            b= dao.addCud(map1,a);

        }



            return b;

    }
    @RequestMapping("/getRuMaMoney")
    public  Map<String, List> getRuMaMoney(LayDate layDate){

        Map<String, List> maps = dao.getRuMaMoney(layDate);
        return maps;
    }
    @RequestMapping("getAllTypeAndMoney")
    public List<Map>  getAllTypeAndMoney(){
        List<Map> maps = dao.getAllTypeAndMoney();
        return maps;
    }
    @RequestMapping("queryDingdDan")
    public int queryDingdDan(HttpSession session) {
        int i = dao.queryDingdDan();
        session.setAttribute("chuding", i);
        return i;
    }
    @RequestMapping("/jianK")
    public int jianK(Integer rkid) {

        List<Map> maps = dao.queryRuKuDatil(rkid);

        int c = 0;
        int a = 0;
        for (Map map : maps) {
            c += dao.addS(map);
        }

        if (c == maps.size()) {
            a = dao.upda(rkid);
        }

        return a;
    }
}
